package wenjie.chen.receiver.controller;

import org.bytesoft.compensable.Compensable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import wenjie.chen.receiver.command.DecreaseAmountCommand;
import wenjie.chen.receiver.command.IncreaseAmountCommand;
import wenjie.chen.receiver.commandHandler.IAccountCommandHandler;

import javax.validation.Valid;

@RestController
@Transactional
@Compensable(
    interfaceClass = IAccountCommandHandler.class,
    confirmableKey = "accountCommandHandlerConfirm",
    cancellableKey = "accountCommandHandlerCancel")
public class AccountController implements IAccountCommandHandler {
    @Qualifier("accountCommandHandler")
    @Autowired
    private IAccountCommandHandler accountServiceImpl;

    @ResponseBody
    @PostMapping("/increaseAmount")
    @Override
    public void increaseAmount(@Valid @RequestBody IncreaseAmountCommand command) {
        accountServiceImpl.increaseAmount(command);
    }

    @ResponseBody
    @PostMapping("/decreaseAmount")
    @Override
    public void decreaseAmount(@Valid @RequestBody DecreaseAmountCommand command) {
        accountServiceImpl.decreaseAmount(command);
    }
}
